package reentrantlock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo {

    public static void main(String[] args) throws InterruptedException {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock(); // 1
                try {
                    System.out.println("before await"); // 2
                    condition.await(); // 3
                    System.out.println("after await"); // 9
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock(); // 10
                }
            }
        }).start();
        // 为了让上面的线程先获取到锁
        Thread.sleep(1000);
        lock.lock(); // 4
        try {
            //模拟业务耗时
            Thread.sleep(1000);
            System.out.println("before signal"); // 5
            condition.signal(); // 6
            System.out.println("after signal"); // 7
        } finally {
            lock.unlock(); // 8
        }
    }

}
